import { _decorator, Component, Node, Label, tween, view, UITransform, Vec3, SpriteAtlas, SpriteFrame, Sprite, UIOpacity, EventTouch, find } from 'cc';
import { App } from '../App';
import { LoadMg } from '../tools/LoadMg';
import UtilTool from '../tools/UtilTool';
const { ccclass, property } = _decorator;

// 设置多语言
@ccclass('LanguagePanel')
export class LanguagePanel extends Component {
    @property(Node)
    contentNode: Node = null;


    private selLanguageId = 1   // 选择的语音id
   
    onLoad() {
        UtilTool.setScreen(this.node)
        this.contentNode.getComponent(UITransform).setContentSize(view.getVisibleSize())

        UtilTool.nodeActive(this.contentNode, 1, 0.16)
    }

    start() {
        // 切换按钮
        for (let index = 0; index < 4; index++) {
            let toggle = find("bg/btn_toggle" + index,this.contentNode)
            toggle.on(Node.EventType.TOUCH_END,(event: EventTouch) =>{
                App.Audio.playOneShot("click")
                this.clickSwitch(index)
            })
        }

        this.clickSwitch(App.language.getLanguageId-1,true)
    }

    /** 
     * 切换语言
      */
    clickSwitch(indexID,init=false) {
        if (!init && this.selLanguageId==indexID+1) {
            return
        }

        for (let index = 0; index < 4; index++) {
            let toggle = find("bg/btn_toggle" + index,this.contentNode)
            toggle.getChildByName("nor").active = indexID!=index
            toggle.getChildByName("sel").active = indexID==index
        }
        
        this.selLanguageId = indexID+1

        if (!init) {
            this.clickSure()
        }
    }

    /** 
     * 关闭
      */
    clickClose() {
        App.Audio.playOneShot("click")
        this.node.destroy();

        // this.clickSure()
    }

    /** 
     * 确定切换语言
      */
    clickSure() {
        App.language.changeLanguage(this.selLanguageId)
        this.node.destroy();
    }
}

